package com.pet.action.wys;

import java.io.*;
import java.sql.SQLException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.pet.bean.User;
import com.pet.dao.imp.OperateDbImp;
import com.pet.dao.wys.UserDao;

/**
 * 类名称 ： InitAction
 * 类描述 ：首页操作action
 * 创建人 ： wangys
 * 分机号 ： 6721
 * 创建时间 ： 2013-3-17下午05:37:10
 *
 */
public class InitAction extends ActionSupport {
	
	private static final Log log = LogFactory.getLog(InitAction.class);
	private static final String IMG_PATH = "/upload/";
	private List<File> file;
    private List<String> fileFileName;
    private List<String> fileContentType;
	
	private UserDao userDao;
	private User user;
	//用户列表
	private List userList;
	private String totalUser = "0";
	private String totalForum = "0";
	
	//最近帖子列表
	private List forumList;
	
	public String queryInit(){
		log.info("查询最近注册的用户....");
		userList = userDao.queryInit();
		
		log.info("查询最近所发的帖子....");
		forumList = userDao.queryForum();
		
		totalUser = "" + userList.size();
		totalForum = "0";
		
		System.out.println(null == userList);
		
		return "index";
	}
	
	//用户注册
	public String regist(){
		log.info("用户注册--->>>>>");
		
		log.info("图片上传>>>>>");
		
		//得到工程保存图片的路径
        String root = ServletActionContext.getRequest().getRealPath(IMG_PATH);
        
        //循环上传的文件
        for(int i = 0 ; i < file.size() ; i ++){
            InputStream is;
			try {
				is = new FileInputStream(file.get(i));
	            //得到图片保存的位置(根据root来得到图片保存的路径在tomcat下的该工程里)
	//            File destFile = new File(root,this.getFileFileName().get(i));
				String fname = getFileName();
	            File destFile = new File(root,fname);
	            
	            //把图片写入到上面设置的路径里
	            OutputStream os = new FileOutputStream(destFile);
	            byte[] buffer = new byte[400];
	            int length  = 0 ;
	            while((length = is.read(buffer))>0){
	                os.write(buffer, 0, length);
	            }
	            is.close();
	            os.close();
	            
            	user.setImg(IMG_PATH + fname);
    			userDao.insertUser(user);
    			
			} catch (Exception e) {
				e.printStackTrace();
				return "err";
			}
        }
		return "registSuc";
	}
	
	/*
	 * 检查注册账号是否可用
	 * */
	public String checkUserId(){
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			boolean s = false;
			try {
				s = userDao.checkUserId(user.getUserId());
				log.info("返回的结果：" + s);
			} catch (SQLException e) {
				log.error("SQL异常");
				e.printStackTrace();
			}
			response.setContentType("text/plain;charset=UTF-8"); 
			if (s){
				response.getWriter().write("true");
			}else{
				response.getWriter().write("false");
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	//登录
	public String login(){
		HttpServletResponse response = ServletActionContext.getResponse();
		
		user = userDao.login(user);
		log.info("登录验证结果：" + (null != user));
		if(null != user){
			try {
				response.getWriter().write(user.getUserId()+"&"+user.getNickname());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}else{
			response.setContentType("text/plain;charset=UTF-8"); 
			try {
				response.getWriter().write("err");
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	//登录
	public String registInit(){
		return "registInit";
	}
	
	
	//测试
	public String test(){
		HttpServletResponse response = ServletActionContext.getResponse();
		try {
			response.setContentType("text/plain;charset=UTF-8"); 
			response.getWriter().write("response 的数据");
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public String getFileName(){
		Date date = new Date();
		SimpleDateFormat df = new SimpleDateFormat("yyyyMMddhhmmss");
		String fileN = df.format(date) + Math.round(2) + ".png";
		log.info("上传的图片名：" + fileN);
		return fileN;
	}
	
	/*
	 * getter and setter
	 * */
	public UserDao getUserDao() {
		return userDao;
	}
	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	public List getUserList() {
		return userList;
	}
	public void setUserList(List userList) {
		this.userList = userList;
	}
	public String getTotalUser() {
		return totalUser;
	}
	public void setTotalUser(String totalUser) {
		this.totalUser = totalUser;
	}
	public String getTotalForum() {
		return totalForum;
	}
	public void setTotalForum(String totalForum) {
		this.totalForum = totalForum;
	}
	public List getForumList() {
		return forumList;
	}
	public void setForumList(List forumList) {
		this.forumList = forumList;
	}
	public User getUser() {
		return user;
	}
	public void setUser(User user) {
		this.user = user;
	}

	public List<File> getFile() {
		return file;
	}

	public void setFile(List<File> file) {
		this.file = file;
	}

	public List<String> getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(List<String> fileFileName) {
		this.fileFileName = fileFileName;
	}

	public List<String> getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(List<String> fileContentType) {
		this.fileContentType = fileContentType;
	}
}
